package com.hemerasolutions.v2trainer.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.hemerasolutions.v2trainer.dao.interfaces.ModalityDaoInterface;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.modality.Modality_;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality_;
import com.hemerasolutions.v2trainer.model.training.TrainingType;

@Repository(value = "modalityDao")
public class ModalityDao extends BaseDao<Modality> implements ModalityDaoInterface  {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	public List<Modality> search(Map<String, Object> fields) {

		List<Predicate> predicateList = new ArrayList<Predicate>();

		CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder();

		CriteriaQuery<Modality> cq  = cb.createQuery(Modality.class);
		
		Root<Modality> modality = cq.from(Modality.class);

		Predicate pred;

		
	    for (Map.Entry<String,Object> line : fields.entrySet()) {
	    	
	    	if(line.getKey().toString() == "name"){
	    		pred = cb.like(cb.upper(modality.get(Modality_.name)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    		

	    	}else if(line.getKey().toString() == "description"){
	    		pred = cb.like(cb.upper(modality.get(Modality_.description)), "%"+line.getValue().toString().toUpperCase()+"%");
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "trainingTypes" ){
	    		Expression<TrainingType> lib =  modality.get(Modality_.trainingType);
	    		pred = lib.in((List<TrainingType>)line.getValue());
	    		predicateList.add(pred);    		
        	
	    	}else if( line.getKey().toString() == "trainingType" ){
	    		pred = cb.equal(modality.get(Modality_.trainingType),(TrainingType)line.getValue() );
	    		predicateList.add(pred);    		
        	
		    	
	    	}else if( line.getKey().toString() == "begChangedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(modality.get(Modality_.lastChange), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "endChangedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(modality.get(Modality_.lastChange), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "begCreatedAt" ){ 
	    		pred = cb.greaterThanOrEqualTo(modality.get(Modality_.createdAt), this.begDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "endCreatedAt" ){ 
	    		pred = cb.lessThanOrEqualTo(modality.get(Modality_.createdAt), this.endDayDateHour((Date)line.getValue()));
	    		predicateList.add(pred);    		
	    	
	    	}else if( line.getKey().toString() == "librarys" ){
	    		Path<VersionableModality> vm = modality.get(Modality_.versionableModality);
	    		
	    		Expression<Library> lib =  vm.get(VersionableModality_.library);
	    		pred = lib.in((List<Library>)line.getValue());
	    		predicateList.add(pred);    
	    	}	
	    }
	    
	    Predicate[] predicates = new Predicate[predicateList.size()];
	    predicateList.toArray(predicates);
	    cq.where(predicates);
	 
	    
	    return super.getEntityManager().createQuery(cq).getResultList();			
		
		
	}
	
	
	
	
}
